package com.dwxt.cdms.api.controller;

import com.dwxt.cdms.api.common.utils.StringUtil;
import com.dwxt.cdms.api.service.InformationService;
import com.dwxt.cdms.entity.info.InformationWithBLOBs;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.IDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cdms/information")
public class InfomationContrller {

    @Autowired
    private InformationService infomationService;

    @PostMapping("/insertOrUpdate")
    private BaseResult insertOrUpdate(InformationWithBLOBs information){
        if (information.getId()!=null){
            infomationService.update(information);
            return BaseResult.ok();
        }else {
            information.setId(IDUtils.genItemId());
            infomationService.insert(information);
            return BaseResult.ok();
        }
    }

    @GetMapping("/delete")
    private BaseResult delete(String id){
        if (id!=null){
            //还未删除视频物理地址，需要开发
            int delete = infomationService.delete(id);
            if (delete>0){
                return BaseResult.ok();
            }else {
                return BaseResult.build(StatusCode.FAIL);
            }
        }
        return BaseResult.build(StatusCode.PARAMETER_WRONG);
    }

    @GetMapping("/selectById")
    private BaseResult selectById(String id){
        if (StringUtil.isNotEmpty(id)){
            InformationWithBLOBs informationWithBLOBs = infomationService.selectById(id);
            return BaseResult.ok(informationWithBLOBs);
        }
        return BaseResult.build(StatusCode.PARAMETER_WRONG);
    }

    @GetMapping("/selectList")
    private BaseResult selectList(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = true, defaultValue = "10") Integer pageSize,InformationWithBLOBs information){
        PageHelper.startPage(pageNum,pageSize);
        List<InformationWithBLOBs> informationWithBLOBs = infomationService.selectByObj(information);
        return BaseResult.ok(new PageInfo<>(informationWithBLOBs));
    }

}
